package com.rightsidetech.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.system.dtos.request.news.NewsTypeReq;
import com.rightsidetech.system.dtos.request.news.NewsTypeSaveReq;
import com.rightsidetech.system.dtos.request.news.NewsTypeUpdateReq;
import com.rightsidetech.system.dtos.response.news.NewsTypePageResp;
import com.rightsidetech.system.entity.news.SysNewsType;
import com.rightsidetech.system.mapper.SysNewsMapper;
import com.rightsidetech.system.mapper.SysNewsTypeMapper;
import com.rightsidetech.system.service.SysNewsTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.Date;
import java.util.List;

@Service("sysNewsTypeService")
public class SysNewsTypeServiceImpl implements SysNewsTypeService {

    @Resource
    private SysNewsTypeMapper sysNewsTypeMapper;
    @Resource
    private SysNewsMapper sysNewsMapper;

    @Override
    public ResponseData save(NewsTypeSaveReq req, String createrName, Long createrId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long count = sysNewsTypeMapper.selectCountByName(req.getTypeName());
        if(count > 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "此分类名称已存在不可重复添加");
        }
        SysNewsType record = getNewsType(req, createrName, createrId);
        record.setCreatedAt(new Date());
        if (sysNewsTypeMapper.insertSelective(record) <= 0) {
            return ResponseData.fail("添加失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ResponseData update(NewsTypeUpdateReq req, String updaterName, Long updaterId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysNewsType newsType = sysNewsTypeMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(newsType)){
            return ResponseData.fail("未找到需要修改的新闻分类");
        }
        if(!newsType.getTypeName().equals(req.getTypeName())){
            Long count = sysNewsTypeMapper.selectCountByName(req.getTypeName());
            if(count > 0){
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "此分类名称已存在不可重复添加");
            }
        }
        SysNewsType record = getNewsType(req, null, null);
        record.setId(req.getId());
        record.setUpdatedAt(new Date());
        record.setUpdaterId(updaterId);
        record.setUpdaterName(updaterName);
        if (sysNewsTypeMapper.updateByPrimaryKeySelective(record) <= 0) {
            return ResponseData.fail("更新失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    private SysNewsType getNewsType(NewsTypeSaveReq req, String createrName, Long createrId){
        SysNewsType newsType = new SysNewsType();
        newsType.setTypeName(req.getTypeName());
        newsType.setCreaterName(createrName);
        newsType.setCreaterId(createrId);
        newsType.setIsDeleted((byte)0);
        return newsType;
    }


    @Override
    public ResponseData<PageInfo<NewsTypePageResp>> page(PageRequest req) {
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<NewsTypePageResp> result = sysNewsTypeMapper.selectPageList();
        return ResponseData.success(new PageInfo<>(result));
    }

    @Override
    public ResponseData<List<NewsTypePageResp>> selectList() {
        return ResponseData.success(sysNewsTypeMapper.selectPageList());
    }

    @Override
    public ResponseData<List<NewsTypePageResp>> selectAppList(NewsTypeReq newsTypeReq){
        List<NewsTypePageResp> list = sysNewsTypeMapper.selectAppListById(newsTypeReq);
        return ResponseData.success(list);
    }

    @Override
    public ResponseData del(DelReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysNewsType newsType = sysNewsTypeMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(newsType) || newsType.getIsDeleted() == 1){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "没有找到需要删除的数据");
        }
        Long count = sysNewsMapper.selectCountByTypeId(newsType.getId());
        if(count > 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "有新闻使用了该分类不允许删除");
        }
        newsType = new SysNewsType();
        newsType.setId(req.getId());
        newsType.setIsDeleted((byte)1);
        if (sysNewsTypeMapper.updateByPrimaryKeySelective(newsType) <= 0) {
            return ResponseData.fail("操作失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }
}
